package com.guigu.service_user.controller;


import com.guigu.common_util.pojo.utils.Result;
import com.guigu.service_user.entity.TbAddress;
import com.guigu.service_user.service.ITbAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@RestController
@RequestMapping("/service_user/tb-address")
public class TbAddressController {
    @Autowired
    private ITbAddressService addressService;

    // 分页条件查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody(required = false) Map<String, Object> searchMap, Integer page, Integer size) {
        return addressService.findPage(searchMap, page, size);
    }
    //增加
    @PostMapping("/insert")
    public Result insert(@RequestBody TbAddress album) {
        boolean save = addressService.save(album);
        if (save){
            return Result.ok();
        }
        return Result.error();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody TbAddress album) {
        boolean update = addressService.updateById(album);
        if (update) {
            return Result.ok();
        }
        return Result.error();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(Integer id) {
        boolean remove = addressService.removeById(id);
        if (remove){
            return Result.ok();
        }
        return Result.error();
    }
    //查询单个
    @GetMapping("/findById")
    public Result findById(Integer id) {
        TbAddress album = addressService.getById(id);
        if (album==null){
            return Result.error();
        }
        return Result.ok().data("address",album);
    }
    @GetMapping("/selectProvincesId")
    public String selectProvincesId(@RequestParam("province") String province){
        return this.addressService.provinces(province);
    }
    @GetMapping("/selectCityId")
    public String selectCityId(@RequestParam("city") String city){
        return this.addressService.cities(city);
    }
    @GetMapping("/selectCountyId")
    public String selectCountyId(@RequestParam("county") String county){
        return  this.addressService.areas(county);
    }

    /**
     * 根据用户名查询地址列表
     * @return
     */
    @GetMapping("/findAddressList")
    public List<TbAddress> findAddressList(String username){
        System.out.println(username);
        System.out.println(addressService.findByUsername(username));
        return addressService.findByUsername(username);
    }
}

